
/** 
* @Description: (用一句话描述该文件做什么) 
* @author heliang
* @date 2019-8-28 下午3:14:20 
* @version Bate1.0
*/

package org.alipay4j.component.life;

import java.util.HashMap;
import java.util.Map;

import org.alipay4j.Alipay;
import org.alipay4j.AlipayException;
import org.alipay4j.Configuration;
import org.alipay4j.component.AbstractComponent;
import org.alipay4j.http.AlipayHttpsClient;
import org.alipay4j.http.Response;
import org.alipay4j.util.ObjectHelper;

import com.alibaba.fastjson.JSONObject;


/** 
 * @ClassName: 生活号操作组件 
 * @Description: 生活号相关操作
 * @author heliang
 * @date 2019-8-28 下午3:14:20 
 * @version V2.1 * Update Logs: * Name: * Date: * Description: 初始化
 */
public class LifeSettingComponent extends AbstractComponent {

    public LifeSettingComponent(Alipay alipay) {
        super(alipay);
    }
    
    /**
    * @Title: 下架生活号 
    * @Description: 下架生活号
    * @author heliang
    * @return result 提示内容
    * @throws AlipayException 
     */
    public String debark() throws AlipayException {
        //组装请求参数
        Map<String, String> body = new HashMap<String, String>();
        body.put("method", "alipay.open.public.life.debark.apply");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_life_debark_apply_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_life_debark_apply_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        return bodyRes.getString("result");
    }
    
    /**
    * @Title: 上架生活号 
    * @Description: 上架生活号 
    * @author heliang
    * @return result 消息提示
    * @throws AlipayException 
     */
    public String aboard() throws AlipayException {
        //组装请求参数
        Map<String, String> body = new HashMap<String, String>();
        body.put("method", "alipay.open.public.life.aboard.apply");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_life_aboard_apply_response");
        if (Configuration.isDebug()) {
          System.out.println("alipay_open_public_life_aboard_apply_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        return bodyRes.getString("result");
    }
    
    /**
    * @Title: 生活号查询行业设置接口 
    * @Description: 生活号查询行业设置接口
    * @author heliang
    * @return {"primary_category":"返回已设置的一级行业分类名称","secondary_category":"返回已设置的二级行业分类名称"}
    * @throws AlipayException 
     */
    public JSONObject categoryQuery() throws AlipayException {
        //组装请求参数
        Map<String, String> body = new HashMap<String, String>();
        body.put("method", "alipay.open.public.setting.category.query");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_setting_category_query_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_setting_category_query_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        return bodyRes;
    }
    
    /**
    * @Title: 生活号基础信息修改接口 
    * @Description: 适用于口碑商户签约当面付2.0默认创建的服务窗（名称为“服务窗+时间戳”，头像为空），系统商需要通过该接口帮助口碑商户修改服务窗名称、头像、欢迎语基础信息，修改名称、头像需要上传相关资质材料，触发风控审核
    * @author heliang
    * @param app_name 生活号名称
    * @param logo_url 生活号头像地址
    * @param public_greeting 欢迎语
    * @param license_url 营业执照地址，
    * @param shop_pics 门店照片地址
    * @param auth_pic 授权运营书图片地址
    * @param introduction 简介
    * @param background_url 背景图片地址
    * @return JSONObject : {"code": "10000","msg": "Success","audit_status": "AUDITING","audit_desc": "审核中"}
    * @throws AlipayException 
     */
    public JSONObject infoModify(String app_name,String logo_url,String public_greeting,String license_url,String shop_pics,
            String auth_pic,String introduction,String background_url) throws AlipayException {
        //组装请求参数
        Map<String, String> body = new HashMap<String, String>();
        body.put("app_name", app_name);
        body.put("logo_url", logo_url);
        body.put("public_greeting", public_greeting);
        body.put("license_url", license_url);
        body.put("shop_pics", shop_pics);
        body.put("auth_pic", auth_pic);
        body.put("introduction", introduction);
        body.put("background_url", background_url);
        body.put("method", "alipay.open.public.info.modify");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_info_modify_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_info_modify_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        return bodyRes;
    }
    
    /**
    * @Title: 添加收款账号接口 
    * @Description: 允许开发者调用该接口将收款账号与当前生活号绑定，从而支持支付成功页引导推荐关注生活号
    * @author heliang
    * @param login_id 收款账号，需要绑定的收款支付宝账号，跟pid不要同时传
    * @param pid 支付宝用户id，2088开头的16位长度字符串，跟login_id不要同时传
    * @return boolean
    * @throws AlipayException 
     */
    public boolean payeeBindCreate(String login_id,String pid) throws AlipayException {
        if (ObjectHelper.isEmpty(login_id)) {
            throw new IllegalArgumentException("login_id can't be null or empty");
        }
        //组装请求参数
        Map<String, String> body = new HashMap<String, String>();
        body.put("login_id", login_id);
        body.put("pid", pid);
        body.put("method", "alipay.open.public.payee.bind.create");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_payee_bind_create_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_payee_bind_create_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        return true;
    }
    
    /**
     * @Title: 解绑收款账号接口)
     * @Description: 允许开发者调用该接口将收款账号与当前生活号解绑
     * @author heliang
     * @param login_id 收款账号，需要绑定的收款支付宝账号，跟pid不要同时传
     * @param pid 支付宝用户id，2088开头的16位长度字符串，跟login_id不要同时传
     * @return boolean
     * @throws AlipayException 
      */
    public boolean payeeBindDelete(String login_id,String pid) throws AlipayException {
        if (ObjectHelper.isEmpty(login_id)) {
            throw new IllegalArgumentException("login_id can't be null or empty");
        }
        //组装请求参数
        Map<String, String> body = new HashMap<String, String>();
        body.put("login_id", login_id);
        body.put("pid", pid);
        body.put("method", "alipay.open.public.payee.bind.delete");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_payee_bind_delete_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_payee_bind_delete_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        return true;
    }
}
